package com.lc1993929.dubbo.boot;

import com.alibaba.dubbo.config.*;
import com.lc1993929.dubbo.boot.reference.DubboReferenceBeanPostProcessor;
import com.lc1993929.dubbo.boot.service.DubboScanAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * Created by LiuChang on 2017/9/12/012.
 * dubbo的springboot自动配置类
 */
@Configuration
@EnableConfigurationProperties(DubboProperties.class)
@Import({DubboScanAutoConfiguration.class, DubboReferenceBeanPostProcessor.class})
public class DubboAutoConfiguration {

    @Autowired
    private DubboProperties dubboProperties;

    @Bean
    @ConditionalOnMissingBean(ApplicationConfig.class)
    public ApplicationConfig getApplicationConfig() {
        return dubboProperties.getApplication();
    }

    @Bean
    @ConditionalOnMissingBean(RegistryConfig.class)
    public RegistryConfig getRegistryConfig() {
        return dubboProperties.getRegistry();
    }

    @Bean
    @ConditionalOnMissingBean(ProtocolConfig.class)
    public ProtocolConfig getProtocolConfig() {
        return dubboProperties.getProtocol();
    }

    @Bean
    @ConditionalOnMissingBean(MonitorConfig.class)
    @ConditionalOnProperty(prefix = "spring.dubbo.monitor", name = "protocol")
    public MonitorConfig getMonitorConfig() {
        return dubboProperties.getMonitor();
    }

    @Bean
    @ConditionalOnMissingBean(ModuleConfig.class)
    @ConditionalOnProperty(prefix = "spring.dubbo.module", name = "name")
    public ModuleConfig getModuleConfig() {
        return dubboProperties.getModule();
    }

    @Bean
    @ConditionalOnMissingBean(ProviderConfig.class)
    public ProviderConfig getProviderConfig() {
        return dubboProperties.getProvider();
    }

    @Bean
    @ConditionalOnMissingBean(ConsumerConfig.class)
    public ConsumerConfig getConsumerConfig() {
        return dubboProperties.getConsumer();
    }

}
